/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.yydcdut.sdlv.sample.slice;

import com.yydcdut.sdlv.Menu;
import com.yydcdut.sdlv.MenuItem;
import com.yydcdut.sdlv.SlideAndDragListView;
import com.yydcdut.sdlv.sample.ResourceTable;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.colors.RgbColor;
import ohos.agp.components.*;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.agp.window.dialog.ToastDialog;

import java.util.ArrayList;

/**
 * 左右滑动菜单
 */
public class SlideDragListAbilitySlice extends AbilitySlice implements SlideAndDragListView.OnDragDropListener, SlideAndDragListView.OnSlideListener,
        SlideAndDragListView.OnMenuItemClickListener, SlideAndDragListView.OnItemDeleteListener,
        SlideAndDragListView.OnItemScrollBackListener {

    private SlideAndDragListView mListView;
    private Menu mMenu;
    private ListItemEnity mDraggedEntity;
    private ArrayList<ListItemEnity> mAppList = new ArrayList<>();
    private int menuWith = 150, menuHeight = 180, menuBtnHeight = 180, menuBtnWith = 150, marginTop = 25;


    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_drag_del);
        initData();
        initMenu();
        initUiAndListener();
    }

    public void initData() {
        ListItemEnity listItemEnity = new ListItemEnity();
        listItemEnity.setItemLabel("111111");
        listItemEnity.setItemImageId(ResourceTable.Media_icon);
        mAppList.add(listItemEnity);
        listItemEnity = new ListItemEnity();
        listItemEnity.setItemLabel("2222222");
        listItemEnity.setItemImageId(ResourceTable.Media_icon);
        mAppList.add(listItemEnity);
        listItemEnity = new ListItemEnity();
        listItemEnity.setItemLabel("3333333");
        listItemEnity.setItemImageId(ResourceTable.Media_icon);
        mAppList.add(listItemEnity);
        listItemEnity = new ListItemEnity();
        listItemEnity.setItemLabel("4444444");
        listItemEnity.setItemImageId(ResourceTable.Media_icon);
        mAppList.add(listItemEnity);
        listItemEnity = new ListItemEnity();
        listItemEnity.setItemLabel("5555555");
        listItemEnity.setItemImageId(ResourceTable.Media_icon);
        mAppList.add(listItemEnity);
        listItemEnity = new ListItemEnity();
        listItemEnity.setItemLabel("66666666");
        listItemEnity.setItemImageId(ResourceTable.Media_icon);
        mAppList.add(listItemEnity);
        listItemEnity = new ListItemEnity();
        listItemEnity.setItemLabel("7777777");
        listItemEnity.setItemImageId(ResourceTable.Media_icon);
        mAppList.add(listItemEnity);

    }

    public void initMenu() {
        mMenu = new Menu(true);
        ShapeElement shapeElement = new ShapeElement();
        shapeElement.setRgbColor(convertColor(0xffff4444));
        ShapeElement shapeElementGreen = new ShapeElement();
        shapeElementGreen.setRgbColor(convertColor(0xff669900));
        ShapeElement shapeElementBlue = new ShapeElement();
        shapeElementBlue.setRgbColor(convertColor(0xff0099cc));
        ShapeElement shapeElementYellow = new ShapeElement();
        shapeElementYellow.setRgbColor(convertColor(0xffff8800));
        mMenu.addItem(new MenuItem.Builder().setWidth(menuWith)
                .setHeight(menuHeight)
                .setMarginTop(marginTop)
                .setBackground(shapeElementYellow)
                .setText("One")
                .setTextColor(Color.WHITE.getValue())
                .setTextSize(40)
                .setMenuBtnHeight(menuBtnHeight)
                .setMenuBtnWidth(menuBtnWith)
                .build());
        mMenu.addItem(new MenuItem.Builder().setWidth(menuWith)
                .setHeight(menuHeight)
                .setMarginTop(marginTop)
                .setBackground(shapeElementGreen)
                .setText("Two")
                .setTextColor(Color.WHITE.getValue())
                .setTextSize(40)
                .setMenuBtnHeight(menuBtnHeight)
                .setMenuBtnWidth(menuBtnWith)
                .build());
        mMenu.addItem(new MenuItem.Builder().setWidth(menuWith)
                .setHeight(menuHeight)
                .setMarginTop(marginTop)
                .setBackground(shapeElementBlue)
                .setText("Three")
                .setDirection(MenuItem.DIRECTION_RIGHT)
                .setTextColor(Color.WHITE.getValue())
                .setTextSize(40)
                .setMenuBtnHeight(menuBtnHeight)
                .setMenuBtnWidth(menuBtnWith)
                .build());
        mMenu.addItem(new MenuItem.Builder().setWidth(menuWith)
                .setHeight(menuHeight)
                .setMarginTop(marginTop)
                .setBackground(shapeElement)
                .setText("删除")
                .setTextSize(40)
                .setTextColor(Color.WHITE.getValue())
                .setDirection(MenuItem.DIRECTION_RIGHT)
                .setMenuBtnHeight(menuBtnHeight)
                .setMenuBtnWidth(menuBtnWith)
                .build());
    }

    public void initUiAndListener() {
        mListView = (SlideAndDragListView) findComponentById(ResourceTable.Id_lv_edit);
        DirectionalLayout componentContainer = (DirectionalLayout) findComponentById(ResourceTable.Id_dl_group);
        mListView.initView(componentContainer);
        mListView.setMenu(mMenu);
        mListView.setItemProvider(mAdapter);
//        mListView.setOnScrollListener(this);
        mListView.setOnDragDropListener(this);
//        mListView.setOnItemClickListener(this);
        mListView.setOnSlideListener(this);
        mListView.setOnMenuItemClickListener(this);
        mListView.setOnItemDeleteListener(this);
//        mListView.setOnItemLongClickListener(new );
        mListView.setOnItemScrollBackListener(this);
    }

    private BaseItemProvider mAdapter = new BaseItemProvider() {

        @Override
        public int getCount() {
            return mAppList.size();
        }

        @Override
        public Object getItem(int position) {
            return mAppList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return mAppList.get(position).hashCode();
        }


        @Override
        public Component getComponent(int position, Component convertView, ComponentContainer componentContainer) {
            CustomViewHolder cvh;
            if (convertView == null) {
                cvh = new CustomViewHolder();
                convertView = LayoutScatter.getInstance(SlideDragListAbilitySlice.this).parse(ResourceTable.Layout_item_custom_btn, null, false);
                cvh.imgLogo = (Image) convertView.findComponentById(ResourceTable.Id_img_item_edit);
                cvh.txtName = (Text) convertView.findComponentById(ResourceTable.Id_txt_item_edit);
                cvh.btnClick = (Button) convertView.findComponentById(ResourceTable.Id_btn_item_click);
                cvh.btnClick.setClickedListener(mOnClickListener);
                convertView.setTag(cvh);
            } else {
                cvh = (CustomViewHolder) convertView.getTag();
            }
            ListItemEnity item = (ListItemEnity) this.getItem(position);
            cvh.txtName.setText(item.getItemLabel());
            cvh.imgLogo.setPixelMap(item.getItemImageId());
            cvh.btnClick.setText(position + "");
            cvh.btnClick.setTag(position);
            return convertView;

        }

        class CustomViewHolder {
            public Image imgLogo;
            public Text txtName;
            public Button btnClick;
        }

        private Component.ClickedListener mOnClickListener = new Component.ClickedListener() {

            @Override
            public void onClick(Component component) {
                Object o = component.getTag();
                if (o instanceof Integer) {
                    new ToastDialog(getContext()).setText("button click-->" + (Integer) o).show();
                }
            }
        };
    };

    private int mBeginPosition;

    @Override
    public void onDragViewStart(int beginPosition) {
        mBeginPosition = beginPosition;
        mDraggedEntity = mAppList.get(beginPosition);
    }

    @Override
    public void onDragDropViewMoved(int fromPosition, int toPosition) {
    }

    @Override
    public void onDragViewDown(int finalPosition) {
        ListItemEnity applicationInfo = mAppList.remove(mBeginPosition);
        mAppList.add(finalPosition, applicationInfo);
        mListView.setItemProvider(mAdapter);
        mListView.getItemProvider().notifyDataChanged();
    }

    @Override
    public void onItemMoveFromTo(int fromPosition, int toPosition) {
        //TODO 长按拖动回调
        ListItemEnity applicationInfo = mAppList.remove(fromPosition);
        mAppList.add(toPosition, applicationInfo);
        mListView.getItemProvider().notifyDataChanged();
    }

    @Override
    public void onSlideOpen(Component view, Component parentView, int position, int direction) {
    }

    @Override
    public void onSlideClose(Component view, Component parentView, int position, int direction) {
    }

    @Override
    public int onMenuItemClick(Component v, int itemPosition, int buttonPosition, int direction) {
        switch (direction) {
            case MenuItem.DIRECTION_LEFT:
                switch (buttonPosition) {
                    case 0:
                        return Menu.ITEM_NOTHING;
                    case 1:
                        return Menu.ITEM_SCROLL_BACK;
                }
                break;
            case MenuItem.DIRECTION_RIGHT:
                switch (buttonPosition) {
                    case 0:
                        return Menu.ITEM_SCROLL_BACK;
                    case 1:
                        return Menu.ITEM_DELETE_FROM_BOTTOM_TO_TOP;
                }
        }
        return Menu.ITEM_NOTHING;
    }

    @Override
    public void onItemDeleteAnimationFinished(Component view, int position) {
        mAppList.remove(position);
        mAdapter.notifyDataChanged();
        mListView.getItemProvider().notifyDataSetItemRemoved(position);
        mListView.getItemProvider().notifyDataChanged();
    }


    public RgbColor convertColor(int color) {
        int colorInt = color;
        int red = (colorInt & 0xff0000) >> 16;
        int green = (colorInt & 0x00ff00) >> 8;
        int blue = (colorInt & 0x0000ff);
        return new RgbColor(red, green, blue);
    }

    @Override
    public void onScrollBackAnimationFinished(Component view, int position) {

    }
}
